﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;        
using System.Net.Mail;
using TimKen.DataEntity;

namespace TimKen.Web.User
{
    public partial class Register : WebPage
    {
        private WebUserManager wum = new WebUserManager();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (Session["CheckCode"] != null && Session["CheckCode"].ToString() == txtCheckCode.Text.Trim().ToLower())
            {
                TimKen.DataEntity.WebUser webuser = new TimKen.DataEntity.WebUser();
                webuser.UserName = txtUserName.Text;
                webuser.NickName = "";
                webuser.Company = txtCompany.Text;
                webuser.Email = txtEmail.Text;
                webuser.Phone = txtPhone.Text;
                webuser.RealName = txtRealName.Text;
                webuser.UserName = txtUserName.Text;
                webuser.Password = txtPassword.Text;
                try
                {
                    //判断用户是否存在(后台已经也做了判断)
                    if (TimKen.BussLogic.UserFacade.Exists(txtUserName.Text))
                    {
                        Alert(GetLangString("UserExist"));
                    }
                    else
                    {
                        wum.Add(webuser);

                        Session.Add("tempUserName", webuser.UserName);

                        //邮箱确认,必须点击邮箱的确认链接,才能够使用户生效
                        //  string TimKenUrl = ConfigurationManager.AppSettings["TimKenUrl"];
                        string TimKenUrl = "http://" + Request.ServerVariables["HTTP_HOST"];
                        string code = Tools.EncryptUserName(webuser.UserName);
                        string path = TimKenUrl + "/User/CheckUser.aspx";
                        string checkcode = "<a href=\"" + path + "?code=" + code + "\">" + GetLangString("ClickHere") + "</a>";
                        MailMessage mm = new MailMessage();
                        MailAddress to = new MailAddress(webuser.Email);
                        mm.To.Add(to);
                        mm.Subject = GetLangString("AuthenticationUserTitle");
                        mm.Body = string.Format(GetLangString("AuthenticationUserContent"), checkcode, path, code);
                        mm.IsBodyHtml = true;
                        Tools.SendEmail(mm);

                       // Alert(GetLangString("EmailSend"), "/User/Login.aspx?UserName=" + webuser.UserName + "&Password=" + txtPassword.Text + "&TxtCode=&intcode=0");
                        // document.location = '/User/Login.aspx?UserName=' + document.getElementById("UserName").value + '&Password=' + document.getElementById("Password").value + '&TxtCode=' + $("#txt_code").val() + '&intcode=' + intclick;

                      Response.Redirect("/User/Login.aspx?UserName=" + webuser.UserName + "&Password=" + txtPassword.Text + "&TxtCode=&intcode=0");
                    }

                }
                catch (CstmException ex)
                {
                   // Alert(ex.Message);
                }
            }
            else
            {
                Msg.Text = TimKen.Localization.LocalizationUtility.GetText("InvalidCheckCode");
            }

        }
    }
}
